﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;
using DevComponents.DotNetBar;

namespace project_assess.prj_view
{
    public partial class ratio_frm : Office2007Form
    {
        Mfrm mfrm;
        DataBase m_db;
        public ratio_frm(Mfrm frm)
        {
            InitializeComponent();
            mfrm = frm;
            m_db = frm.m_db;
        }
        #region 加载单价数据库名称
        void load_combox()
        {
            load_comb1();
            load_comb2();
            load_comb3();
           
          
           
        }
        void load_comb1()
        {
            string sql = "select [日期] from [material_para] group by [日期] order by [日期] desc";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            advTree1.Nodes.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Node n = new Node();
                n.Text = Convert.ToDateTime(dt.Rows[i][0]).ToShortDateString();
                advTree1.Nodes.Add(n);
            }
            advTree1.SelectedNode = advTree1.Nodes[0];
        }
        void load_comb2()
        {
            string sql = "select [日期] from [bury_para] group by [日期] order by [日期] desc";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            advTree2.Nodes.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Node n = new Node();
                n.Text = Convert.ToDateTime(dt.Rows[i][0]).ToShortDateString();
                advTree2.Nodes.Add(n);
            }
            advTree2.SelectedNode = advTree2.Nodes[0];
        }
        void load_comb3()
        {
            string sql = "select [日期] from [workload_para] group by [日期] order by [日期] desc";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            advTree3.Nodes.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Node n = new Node();
                n.Text = Convert.ToDateTime(dt.Rows[i][0]).ToShortDateString();
                advTree3.Nodes.Add(n);
            }
            advTree3.SelectedNode = advTree3.Nodes[0];
        }
        #endregion
        private void ratio_frm_Load(object sender, EventArgs e)
        {
            load_combox();
        }
        #region 点击选择单价数据库
        private void advTree1_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            if (advTree1.SelectedNode == null) return;
            string time = advTree1.SelectedNode.Text;
            string sql = "select [分项内容],[规格方法],[材料单价] from [material_para] where [日期]='"
                + time + "'";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            grid1.DataSource = dt;

        }

        private void advTree2_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            if (advTree2.SelectedNode == null) return;
            string time = advTree2.SelectedNode.Text;
            string sql = "select [分项内容],[规格方法],[埋设单价] from [bury_para] where [日期]='"
                + time + "'";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            grid2.DataSource = dt;
        }

        private void advTree3_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            if (advTree3.SelectedNode == null) return;
            string time = advTree3.SelectedNode.Text;
            string sql = "select [分项内容],[单价],[比例] from [workload_para] where [日期]='"
                + time + "'";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            grid3.DataSource = dt;
        }
        #endregion
        #region 点击更新单价
        private void btn1_Click(object sender, EventArgs e)
        {
            string time = monthCalendarItem1.SelectedDate.ToShortDateString();
            message_frm frm = new message_frm("是否更新材料费单价?", "询问", icon_img.query);
            frm.ShowDialog(this);
            if (frm.yesorno == false)
            {
                frm.Close();
                return;
            }
            
            string sql = "delete from [material_para] where [日期]='" + time + "'";
            m_db.ExecuteSQL(sql);
            for (int i = 0; i < grid1.Rows.Count-1; i++)
            {
                 sql = "insert into [material_para] values('"
                    + grid1.Rows[i].Cells[0].Value.ToString() + "','" + grid1.Rows[i].Cells[1].Value.ToString()
                    + "'," + grid1.Rows[i].Cells[2].Value.ToString() + ",'" + time + "')";
                m_db.ExecuteSQL(sql);
            }
            load_comb1();
            advTree1.SelectedNode = advTree1.Nodes[0];
        }
        private void btn2_Click(object sender, EventArgs e)
        {
            message_frm frm = new message_frm("是否更新埋设费单价?", "询问", icon_img.query);
            frm.ShowDialog(this);
            if (frm.yesorno == false)
            {
                frm.Close();
                return;
            }
            string time =  monthCalendarItem4.SelectedDate.ToShortDateString();
            string sql = "delete from [bury_para] where [日期]='" + time + "'";
            m_db.ExecuteSQL(sql);
            for (int i = 0; i < grid2.Rows.Count-1; i++)
            {
                sql = "insert into [bury_para] values('"
                    + grid2.Rows[i].Cells[0].Value.ToString() + "','" + grid2.Rows[i].Cells[1].Value.ToString()
                    + "'," + grid2.Rows[i].Cells[2].Value.ToString() + ",'" + time + "')";
                m_db.ExecuteSQL(sql);
            }
            load_comb2();
            advTree2.SelectedNode = advTree2.Nodes[0];
        }
        private void btn3_Click(object sender, EventArgs e)
        {
            message_frm frm = new message_frm("是否更新工作量单价?", "询问", icon_img.query);
            frm.ShowDialog(this);
            if (frm.yesorno == false)
            {
                frm.Close();
                return;
            }
            string time = monthCalendarItem5.SelectedDate.ToShortDateString();
            string sql = "delete from [workload_para] where [日期]='" + time + "'";
            m_db.ExecuteSQL(sql);
            for (int i = 0; i < grid3.Rows.Count-1; i++)
            {
                sql = "insert into [workload_para] values('"
                    + grid3.Rows[i].Cells[0].Value.ToString() + "'," + grid3.Rows[i].Cells[1].Value.ToString()
                    + "," + grid3.Rows[i].Cells[2].Value.ToString() + ",'" + time + "')";
                m_db.ExecuteSQL(sql);
            }
            load_comb3();
            advTree3.SelectedNode = advTree3.Nodes[0];
        }
        #endregion
        #region 更改参数
        private void grid1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            int column = e.ColumnIndex;
            message_frm frm;
            if (column < 2)
            {
                frm = new message_frm("无法更改参数名称,只能更改单价值", "错误", icon_img.error);
                frm.ShowDialog(this);
                frm.Close();
                e.Cancel = true;
                return;
            }
           
        }


        private void grid2_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            int column = e.ColumnIndex;
            message_frm frm;
            if (column < 2)
            {
                frm = new message_frm("无法更改参数名称,只能更改单价值", "错误", icon_img.error);
                frm.ShowDialog(this);
                frm.Close();
                e.Cancel = true;
                return;
            }
          
        }

        private void grid3_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            int column = e.ColumnIndex;
            message_frm frm;
            if (column < 1)
            {
                frm = new message_frm("无法更改参数名称,只能更改单价值", "错误", icon_img.error);
                frm.ShowDialog(this);
                frm.Close();
                e.Cancel = true;
                return;
            }
       
        }
        private void grid1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            message_frm frm;
            frm = new message_frm("无法更改,输入数值有误", "错误", icon_img.error);
            frm.ShowDialog(this);
            frm.Close();
            e.Cancel = false;
        }

        private void grid2_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            message_frm frm;
            frm = new message_frm("无法更改,输入数值有误", "错误", icon_img.error);
            frm.ShowDialog(this);
            frm.Close();
            e.Cancel = false;
        }
        private void grid3_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            message_frm frm;
            frm = new message_frm("无法更改,输入数值有误", "错误", icon_img.error);
            frm.ShowDialog(this);
            frm.Close();
            e.Cancel = false;
        }
        #endregion


        private bool godelete(KeyEventArgs e)
        {
            
            if (e.KeyCode == Keys.Delete)
            {
                message_frm frm = new message_frm("是否删除该记录?", "询问", icon_img.query);
                frm.ShowDialog(this);
                if (frm.yesorno == true)
                {
                    frm.Close();
                    return true;
                }
            }
            return false;
        }
        private void advTree1_KeyDown(object sender, KeyEventArgs e)
        {
            if (godelete(e) == true && advTree1.SelectedNode!=null)
            {
                string time = advTree1.SelectedNode.Text;
                string sql = string.Format("delete from [material_para] where [日期]='{0}'", time);
                m_db.ExecuteSQL(sql);
                load_comb1();
            }
        }

        private void advTree2_KeyDown(object sender, KeyEventArgs e)
        {
            if (godelete(e) == true && advTree2.SelectedNode != null)
            {
                string time = advTree2.SelectedNode.Text;
                string sql = string.Format("delete from [bury_para] where [日期]='{0}'", time);
                m_db.ExecuteSQL(sql);
                load_comb2();
            }
        }

        private void advTree3_KeyDown(object sender, KeyEventArgs e)
        {
            if (godelete(e) == true && advTree3.SelectedNode != null)
            {
                string time = advTree3.SelectedNode.Text;
                string sql = string.Format("delete from [workload_para] where [日期]='{0}'", time);
                m_db.ExecuteSQL(sql);
                load_comb3();
            }
        }

  












    }
}
